﻿using System;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Sustainalytics.Claims;
using Sustainalytics.ClientDW.Entities;
using Sustainalytics.DataLayer.Shared;
using Sustainalytics.ESGRatings.BusinessLogic.ESGOnlineReport;
using Sustainalytics.ESGRatings.Service.Models;
using Sustainalytics.Utils;
using Sustainalytics.Auth.Claims;
using Sustainalytics.Utils.WebApi.ErrorCodes;
using System.Web.Http.Cors;

namespace Sustainalytics.ESGRatings.Service.Controllers
{
    [EnableCors(origins: "*", headers: "*", methods: "*")]
    [ClaimsChecker("api/v1/company/esg")]
    [RoutePrefix("api/v1/company")]

    // [AutoInvalidateCacheOutput]
    [LogControllerFilter(true, true, false)]
    public class EsgHistoricalPerformanceSummaryController : ApiController
    {
        /// <summary>
        /// The _service.
        /// </summary>
        private IHistoricalDataBusinessLogicUnit _service;

        public EsgHistoricalPerformanceSummaryController()
        {
            this.Setup();
        }

       // [CacheOutput(ServerTimeSpan = 100, ClientTimeSpan = 0, NoCache = true, MustRevalidate = true, Private = true)]
        [Route("{companyId}/esgHistoricalPerformanceSummary")]
        [UniverseChecker("universeIds", AccountFeature.EsgRatingsOnlineReport)]
        public HttpResponseMessage Get(Guid companyId, [FromUri]int[] universeIds = null)
        {
            try
            {

                var data = this._service.GetSingleByCompanyId(companyId, universeIds).ToDto();
                return this.Request.CreateResponse(HttpStatusCode.OK, data);


            }
            catch (ESGReasearchNotInUniversesException ex)
            {
                return this.Request.CreateResponse(
                    HttpStatusCode.NotFound, 
                    new { errCode = ex.GetType().ToString(), message = ex.Message });
            }
            catch (ESGReasearchNotFoundException ex)
            {
                return this.Request.CreateResponse(
                    HttpStatusCode.NotFound, 
                    new { errCode = ex.GetType().ToString(), message = ex.Message });
            }
            catch (Exception ex)
            {
                CommonEventSource.Log.Exception(ex);
                return this.Request.CreateResponse(
                    HttpStatusCode.InternalServerError, 
                    new { ErrorStatusCode = WebAPIGeneralErrorCodes.UnhandledException });
            }
        }

        /// <summary>
        /// The setup.
        /// </summary>
        private void Setup()
        {
            IRepositoryCollection<CompanyHistoricalPerformance> repository =
                new RepositoryCollection<CompanyHistoricalPerformance>(
                    HistoryDWConnection.GetConnectionString());
            IRepositoryCollection<CorporateDataBasic> clientDWRepositoryCollection =
                new RepositoryCollection<CorporateDataBasic>(
                    ClientDWConnection.GetConnectionString(), "CorporateData");

            this._service = this._service ??
                       new HistoricalDataBusinessLogicUnit(repository);
            this._service.SetCorporateDataRepositorty(clientDWRepositoryCollection);
        }
    }
}
